<template>
  <div v-loading="loading" :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons')
import resize from './mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chartState'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100%'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      loading: false,
      optionData: {
        dateList: []
      }
    }
  },
  watch: {
    chartData() {
      const data = this.chartData.map((item) => { return { name: this.$t(item.dealStatusLabel), value: item.count } })
      this.chart.setOption({
        legend: {
          data: data
        },
        series: [{
          name: this.$t('类型'),
          data: data
        }]
      })
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  mounted() {
    this.initChart()
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, null, { renderer: 'svg', useDirtyRect: false })
       // 绑定一个点击事件
      this.chart.on('click', this.clickFunc)
      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{b}:{c} ({d}%)',
          textStyle: {
            fontSize: this.fitChartFont(12)
          }
        },
        textStyle: {
          fontSize: this.fitChartFont(12)
        },
        legend: {
          type: 'scroll',
          orient: 'vertical',
          y: 'center',
          left: 'left',
          itemWidth: this.fitChartFont(25),
          itemHeight: this.fitChartFont(14),
          data: [this.$t('项目池'), this.$t('投前'), this.$t('投后'), this.$t('暂缓')],
          textStyle: {
            color: '#fff'
          }
        },

        toolbox: {
          show: false,
          feature: {
            mark: { show: true },
            dataView: { show: true, readOnly: false },
            magicType: {
              show: true,
              type: ['pie', 'funnel']
            },
            restore: { show: true },
            saveAsImage: { show: true }
          }
        },
        color: ['#5470c6', '#91cc75', '#fac858', '#ee6666', '#73c0de', '#3ba272', '#fc8452', '#9a60b4', '#ea7ccc'],
        calculable: true,
        series: [
          {
            name: this.$t('排名'),
            type: 'pie',
            radius: [0, this.fitChartFont(66)],
            center: ['60%', '50%'],
            label: {
              normal: {
                show: true,
                formatter: '{b}: {c}'
              }
            },
            data: [
              { value: 70, name: this.$t('项目池') },
              { value: 90, name: this.$t('投前') },
              { value: 110, name: this.$t('投后') },
              { value: 150, name: this.$t('暂缓') }
            ]
          }
        ],
        animationEasing: 'cubicInOut',
        animationDuration: 3000
      })
    },
    clickFunc(param) {
      const clickName = param.data.name // 当前点击对象的name
      this.$emit('clickStateName', clickName)
    }
  }
}
</script>
